<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/layers/accuracy_layer.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/layers</a> - accuracy_layer.cpp<span style="font-size: 80%;"> (source / <a href="accuracy_layer.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">41</td>
            <td class="headerCovTableEntry">53</td>
            <td class="headerCovTableEntryMed">77.4 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">6</td>
            <td class="headerCovTableEntry">14</td>
            <td class="headerCovTableEntryLo">42.9 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #include &lt;functional&gt;</a>
<span class="lineNum">       2 </span>            : #include &lt;utility&gt;
<span class="lineNum">       3 </span>            : #include &lt;vector&gt;
<span class="lineNum">       4 </span>            : 
<span class="lineNum">       5 </span>            : #include &quot;caffe/layers/accuracy_layer.hpp&quot;
<span class="lineNum">       6 </span>            : #include &quot;caffe/util/math_functions.hpp&quot;
<span class="lineNum">       7 </span>            : 
<span class="lineNum">       8 </span>            : namespace caffe {
<a name="9"><span class="lineNum">       9 </span>            : </a>
<span class="lineNum">      10 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      11 </span><span class="lineCov">          1 : void AccuracyLayer&lt;Dtype&gt;::LayerSetUp(</span>
<span class="lineNum">      12 </span>            :   const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      13 </span><span class="lineCov">          1 :   top_k_ = this-&gt;layer_param_.accuracy_param().top_k();</span>
<span class="lineNum">      14 </span>            : 
<span class="lineNum">      15 </span><span class="lineCov">          1 :   has_ignore_label_ =</span>
<span class="lineNum">      16 </span>            :     this-&gt;layer_param_.accuracy_param().has_ignore_label();
<span class="lineNum">      17 </span><span class="lineCov">          1 :   if (has_ignore_label_) {</span>
<span class="lineNum">      18 </span><span class="lineNoCov">          0 :     ignore_label_ = this-&gt;layer_param_.accuracy_param().ignore_label();</span>
<span class="lineNum">      19 </span>            :   }
<span class="lineNum">      20 </span><span class="lineCov">          1 : }</span>
<a name="21"><span class="lineNum">      21 </span>            : </a>
<span class="lineNum">      22 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      23 </span><span class="lineCov">       2101 : void AccuracyLayer&lt;Dtype&gt;::Reshape(</span>
<span class="lineNum">      24 </span>            :   const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom, const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      25 </span><span class="lineCov">       4202 :   CHECK_LE(top_k_, bottom[0]-&gt;count() / bottom[1]-&gt;count())</span>
<span class="lineNum">      26 </span>            :       &lt;&lt; &quot;top_k must be less than or equal to the number of classes.&quot;;
<span class="lineNum">      27 </span><span class="lineCov">       2101 :   label_axis_ =</span>
<span class="lineNum">      28 </span>            :       bottom[0]-&gt;CanonicalAxisIndex(this-&gt;layer_param_.accuracy_param().axis());
<span class="lineNum">      29 </span><span class="lineCov">       2101 :   outer_num_ = bottom[0]-&gt;count(0, label_axis_);</span>
<span class="lineNum">      30 </span><span class="lineCov">       4202 :   inner_num_ = bottom[0]-&gt;count(label_axis_ + 1);</span>
<span class="lineNum">      31 </span><span class="lineCov">       4202 :   CHECK_EQ(outer_num_ * inner_num_, bottom[1]-&gt;count())</span>
<span class="lineNum">      32 </span>            :       &lt;&lt; &quot;Number of labels must match number of predictions; &quot;
<span class="lineNum">      33 </span>            :       &lt;&lt; &quot;e.g., if label axis == 1 and prediction shape is (N, C, H, W), &quot;
<span class="lineNum">      34 </span>            :       &lt;&lt; &quot;label count (number of labels) must be N*H*W, &quot;
<span class="lineNum">      35 </span>            :       &lt;&lt; &quot;with integer values in {0, 1, ..., C-1}.&quot;;
<span class="lineNum">      36 </span><span class="lineCov">       2101 :   vector&lt;int&gt; top_shape(0);  // Accuracy is a scalar; 0 axes.</span>
<span class="lineNum">      37 </span><span class="lineCov">       2101 :   top[0]-&gt;Reshape(top_shape);</span>
<span class="lineNum">      38 </span><span class="lineCov">       2101 :   if (top.size() &gt; 1) {</span>
<span class="lineNum">      39 </span>            :     // Per-class accuracy is a vector; 1 axes.
<span class="lineNum">      40 </span><span class="lineNoCov">          0 :     vector&lt;int&gt; top_shape_per_class(1);</span>
<span class="lineNum">      41 </span><span class="lineNoCov">          0 :     top_shape_per_class[0] = bottom[0]-&gt;shape(label_axis_);</span>
<span class="lineNum">      42 </span><span class="lineNoCov">          0 :     top[1]-&gt;Reshape(top_shape_per_class);</span>
<span class="lineNum">      43 </span><span class="lineNoCov">          0 :     nums_buffer_.Reshape(top_shape_per_class);</span>
<span class="lineNum">      44 </span>            :   }
<span class="lineNum">      45 </span><span class="lineCov">       2101 : }</span>
<a name="46"><span class="lineNum">      46 </span>            : </a>
<span class="lineNum">      47 </span>            : template &lt;typename Dtype&gt;
<span class="lineNum">      48 </span><span class="lineCov">       2100 : void AccuracyLayer&lt;Dtype&gt;::Forward_cpu(const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; bottom,</span>
<span class="lineNum">      49 </span>            :     const vector&lt;Blob&lt;Dtype&gt;*&gt;&amp; top) {
<span class="lineNum">      50 </span>            :   Dtype accuracy = 0;
<span class="lineNum">      51 </span><span class="lineCov">       2100 :   const Dtype* bottom_data = bottom[0]-&gt;cpu_data();</span>
<span class="lineNum">      52 </span><span class="lineCov">       2100 :   const Dtype* bottom_label = bottom[1]-&gt;cpu_data();</span>
<span class="lineNum">      53 </span><span class="lineCov">       2100 :   const int dim = bottom[0]-&gt;count() / outer_num_;</span>
<span class="lineNum">      54 </span><span class="lineCov">       2100 :   const int num_labels = bottom[0]-&gt;shape(label_axis_);</span>
<span class="lineNum">      55 </span><span class="lineCov">       2100 :   if (top.size() &gt; 1) {</span>
<span class="lineNum">      56 </span><span class="lineNoCov">          0 :     caffe_set(nums_buffer_.count(), Dtype(0), nums_buffer_.mutable_cpu_data());</span>
<span class="lineNum">      57 </span><span class="lineNoCov">          0 :     caffe_set(top[1]-&gt;count(), Dtype(0), top[1]-&gt;mutable_cpu_data());</span>
<span class="lineNum">      58 </span>            :   }
<span class="lineNum">      59 </span>            :   int count = 0;
<span class="lineNum">      60 </span><span class="lineCov">     422100 :   for (int i = 0; i &lt; outer_num_; ++i) {</span>
<span class="lineNum">      61 </span><span class="lineCov">     630000 :     for (int j = 0; j &lt; inner_num_; ++j) {</span>
<span class="lineNum">      62 </span>            :       const int label_value =
<span class="lineNum">      63 </span><span class="lineCov">     210000 :           static_cast&lt;int&gt;(bottom_label[i * inner_num_ + j]);</span>
<span class="lineNum">      64 </span><span class="lineCov">     210000 :       if (has_ignore_label_ &amp;&amp; label_value == ignore_label_) {</span>
<span class="lineNum">      65 </span>            :         continue;
<span class="lineNum">      66 </span>            :       }
<span class="lineNum">      67 </span>            :       DCHECK_GE(label_value, 0);
<span class="lineNum">      68 </span>            :       DCHECK_LT(label_value, num_labels);
<span class="lineNum">      69 </span><span class="lineCov">     210000 :       if (top.size() &gt; 1) ++nums_buffer_.mutable_cpu_data()[label_value];</span>
<span class="lineNum">      70 </span><span class="lineCov">     420000 :       const Dtype prob_of_true_class = bottom_data[i * dim</span>
<span class="lineNum">      71 </span><span class="lineCov">     210000 :                                                    + label_value * inner_num_</span>
<span class="lineNum">      72 </span><span class="lineCov">     210000 :                                                    + j];</span>
<span class="lineNum">      73 </span>            :       int num_better_predictions = -1;  // true_class also counts as &quot;better&quot;
<span class="lineNum">      74 </span>            :       // Top-k accuracy
<span class="lineNum">      75 </span><span class="lineCov">    4233740 :       for (int k = 0; k &lt; num_labels &amp;&amp; num_better_predictions &lt; top_k_; ++k) {</span>
<span class="lineNum">      76 </span><span class="lineCov">    2011870 :         num_better_predictions +=</span>
<span class="lineNum">      77 </span><span class="lineCov">    2011870 :           (bottom_data[i * dim + k * inner_num_ + j] &gt;= prob_of_true_class);</span>
<span class="lineNum">      78 </span>            :       }
<span class="lineNum">      79 </span>            :       // check if there are less than top_k_ predictions
<span class="lineNum">      80 </span><span class="lineCov">     210000 :       if (num_better_predictions &lt; top_k_) {</span>
<span class="lineNum">      81 </span><span class="lineCov">     190470 :         ++accuracy;</span>
<span class="lineNum">      82 </span><span class="lineCov">     190470 :         if (top.size() &gt; 1) ++top[1]-&gt;mutable_cpu_data()[label_value];</span>
<span class="lineNum">      83 </span>            :       }
<span class="lineNum">      84 </span><span class="lineCov">     210000 :       ++count;</span>
<span class="lineNum">      85 </span>            :     }
<span class="lineNum">      86 </span>            :   }
<span class="lineNum">      87 </span>            : 
<span class="lineNum">      88 </span>            :   // LOG(INFO) &lt;&lt; &quot;Accuracy: &quot; &lt;&lt; accuracy;
<span class="lineNum">      89 </span><span class="lineCov">       2100 :   top[0]-&gt;mutable_cpu_data()[0] = (count == 0) ? 0 : (accuracy / count);</span>
<span class="lineNum">      90 </span><span class="lineCov">       2100 :   if (top.size() &gt; 1) {</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :     for (int i = 0; i &lt; top[1]-&gt;count(); ++i) {</span>
<span class="lineNum">      92 </span><span class="lineNoCov">          0 :       top[1]-&gt;mutable_cpu_data()[i] =</span>
<span class="lineNum">      93 </span><span class="lineNoCov">          0 :           nums_buffer_.cpu_data()[i] == 0 ? 0</span>
<span class="lineNum">      94 </span><span class="lineNoCov">          0 :           : top[1]-&gt;cpu_data()[i] / nums_buffer_.cpu_data()[i];</span>
<span class="lineNum">      95 </span>            :     }
<span class="lineNum">      96 </span>            :   }
<span class="lineNum">      97 </span>            :   // Accuracy layer should not be used as a loss function.
<span class="lineNum">      98 </span><span class="lineCov">       2100 : }</span>
<a name="99"><span class="lineNum">      99 </span>            : </a>
<span class="lineNum">     100 </span>            : #ifdef CPU_ONLY
<span class="lineNum">     101 </span><span class="lineNoCov">          0 : STUB_GPU(AccuracyLayer);</span>
<span class="lineNum">     102 </span>            : #endif
<a name="103"><span class="lineNum">     103 </span>            : </a>
<span class="lineNum">     104 </span>            : INSTANTIATE_CLASS(AccuracyLayer);
<a name="105"><span class="lineNum">     105 </span><span class="lineCov">          5 : REGISTER_LAYER_CLASS(Accuracy);</span></a>
<span class="lineNum">     106 </span>            : 
<span class="lineNum">     107 </span><span class="lineCov">          3 : }  // namespace caffe</span>
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
